/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package services.implementation;

import entity.Lists;
import java.util.List;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import entity.Notes;
import java.util.Date;
import services.INotesService;

/**
 *
 * @author smile
 */
@Stateless
@LocalBean
public class NotesService extends GenericServices implements INotesService{
    @PersistenceContext(unitName = "snPU")
    private EntityManager em;
    
    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")

    @Override
    public boolean addNote(Object object) {
        return this.insert(object);
    }

    @Override
    public boolean deleteNote(Object object) {
        return this.delete(object);
    }

    @Override
    public boolean updateNote(Object object) {
        return this.update(object);
    }

    @Override
    public List<Notes> getAllNotesType() {
        return em.createNamedQuery("Notes.findAll").getResultList();
    }

    @Override
    public Notes getNoteById(Integer id) {
        return em.find(Notes.class, id);
    }
    
    public List<Notes> getNotesByListId(Lists idList) {
        try{            
            return this.em.createNamedQuery("Notes.findByListId")
                .setParameter("idList", idList)
                .getResultList();           
        }catch (Exception e){
            return null;
        } 
    } 
    
    public List<Notes> getNotesNotComplitedByList(Lists idList) {
        try{            
            return this.em.createNamedQuery("Notes.findAllNotComplited")
                .setParameter("idList", idList)
                .getResultList();
           
        }catch (Exception e){
            return null;
        } 
    }  
    
    public List<Notes> getNotesComplitedByList(Lists idList) {
        try{            
            return this.em.createNamedQuery("Notes.findAllComplited")
                .setParameter("idList", idList)
                .getResultList();
           
        }catch (Exception e){
            return null;
        } 
    }
    
    
    public boolean setComplete (Integer id, Integer complete) {
        try{
            System.out.println(id + " id");
                        System.out.println(complete + " complete");
            this.em.createNamedQuery("Notes.updateCompleteById")
                .setParameter("isComplete", complete)     
                .setParameter("id", id)
                .executeUpdate();
            
                        System.out.println(id + " id");
                        System.out.println(complete + " complete");
                        
                        
           return true;
        }catch (Exception e){
            return false;
        } 
    }
    
    public boolean setPriority (Integer id, Integer priority) {
        try{       
            this.em.createNamedQuery("Notes.updatePriorityByNote")
                .setParameter("isPriority", priority)     
                .setParameter("id", id)
                .executeUpdate();
           return true;
        }catch (Exception e){
            return false;
        } 
    } 
    
    public boolean setExpiresDate (Integer id, Date date) {
        try{       
            this.em.createNamedQuery("Notes.updateExpiresDateById")
                .setParameter("dateExpires", date)     
                .setParameter("id", id)
                .executeUpdate();
           return true;
        }catch (Exception e){
            return false;
        } 
    }
       
        
}
